草庐IT

json - 区分 XBRL、XML、CSV 和 JSON

全部标签

ruby - mongoid 文档 to_json 包括所有嵌入的文档,每个文档都没有 ':include'

给定一个任意的mongoid文档,我如何将其转换为JSON并包含任何嵌入式结构,而不是在我的to_json语句中特别包含这些结构。例如:#!/usr/bin/envrubyrequire'mongoid'require'json'require'pp'classDocincludeMongoid::DocumentincludeMongoid::Timestampsfield:doc_specific_info,type:Stringembeds_many:personsendclassPersonincludeMongoid::Documentfield:role,type:Stri

ruby - 我怎样才能得到 Browser.text.include?不区分大小写?

就这么简单:我怎样才能得到Browser.text.include?,或者一般的Ruby,对指定的命令不区分大小写? 最佳答案 最简单的方法之一是将您正在阅读的文本小写或大写:Browser.text.downcase.include?然后,您需要确保以全部小写形式提供所需的文本。 关于ruby-我怎样才能得到Browser.text.include?不区分大小写?,我们在StackOverflow上找到一个类似的问题: https://stackoverfl

ruby - 在 Rails 应用程序中导入 CSV 时忽略具有空白值的行

我有一个允许用户导入CSV数据的应用程序。一切正常,直到用户开始导入CSV文件中包含空白行的数据。我正在使用以下内容从CSV文件中获取行:CSV.readlines(import_file,headers:true,skip_blanks:true)我认为如果我将选项添加到skip_blanks它会这样做,但事实并非如此。关于如何忽略空白行的任何想法。谢谢! 最佳答案 这应该可行CSV.open(import_file,skip_blanks:true).reject{|row|row.all?(&:nil?)}编辑您请求了read

ruby - 如何使用 XPath 和 Nokogiri 获取 XML 节点的内容

我有这样的代码:@doc=Nokogiri::HTML(open(url)@doc.xpath(query).eachdo|html|putshtml#howgetcontentofanodeend我如何获取节点的内容而不是像这样: 最佳答案 这是READMEfile中的概要示例为Nokogiri展示了一种使用CSS、XPath或混合的方法:require'nokogiri'require'open-uri'#GetaNokogiri::HTML:Documentforthepagewe’reinterestedin...doc=N

ruby-on-rails - gem install json 失败,重新定义了 struct timezone/timespec

我在Windows上使用带有DevKit的Ruby1.9.3(在Win764位上都是32位)。现在我尝试安装rails,但从bundle中得到一个错误。如果我尝试运行(包在提示什么)geminstalljson我收到以下错误消息:D:\RubyTest>geminstalljsonTemporarilyenhancingPATHtoincludeDevKit...Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingjson:ERROR:Failedtobuildgemnativeextension.D:

ruby - 在 Ruby 中,如何从散列输出 json 并给它换行和制表符

我正在尝试格式化{"key"=>"value"}以将其转换为:{"key":"value"}用于写入json文件。现在我正在做:hash={"key"=>"value"}putshash.to_json.gsub('{','{\n\t')开始。这输出{\n\t"key":"value"}为什么我不能换行? 最佳答案 为漂亮的东西欢呼,为避免正则表达式欢呼!使用内置的JSON.pretty_generate方法require'json'putsJSON.pretty_generatehash,options耶!选项如下:indent:

ruby - nil.to_json 无法解析回 nil?

此代码段抛出异常:x=niljsoned=x.to_jsonputs'x.to_json='+jsoned.inspectputs'back='+JSON.parse(jsoned).inspectC:/ruby/lib/ruby/1.9.1/json/common.rb:146:in`parse':706:unexpectedtokenat'null'(JSON::ParserError)x.to_json="null"fromC:/ruby/lib/ruby/1.9.1/json/common.rb:146:in`parse'fromC:/dev/prototyping/appox

ruby-on-rails - 在 ruby​​ on rails 中使用 facebook graph api 时从 url 获取一个 json 对象

如何在我的ruby​​onrails代码中通过URL获取JSON对象:url="https://graph.facebook.com/me/friends?access_token=XXX"我无法获得有关JSON+RubyonRails的良好教程。谁能帮帮我,谢谢。 最佳答案 require'open-uri'require'json'json_object=JSON.parse(open("https://graph.facebook.com/me/friends?access_token=XXX").read)这是最简单的方法,

ruby - 在 ruby​​ 中对 JSON 哈希数组进行排序

所以我有一个哈希数组:[{"id":"30","name":"Dave"},{"id":"57","name":"Mike"},{"id":"9","name":"Kevin"},...{"id":"1","name":"Steve"}]我想按id属性对其进行排序,使其看起来像这样:[{"id":"1","name":"Steve"},{"id":"2","name":"Walter"},...{"id":"60","name":"Chester"}]我假设我使用的是sort_by方法,但我不确定该怎么做。 最佳答案 这应该有效:a

ruby - 使用 Ruby CSV header 转换器

假设我有以下类(class):classBuyer以及CSV文件中的以下内容:FirstName,LastNameJohn,DoeJane,Doe我想将CSV的内容保存到数据库中。我在Rake文件中有以下内容:namespace:migrationdodesc"MigrateCSVdata"task:import,[:model,:file_path]=>:environmentdo|t,args|require'csv'model=args.model.constantizepath=args.file_pathCSV.foreach(path,:headers=>true,:con